package com.fei.socket;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class Response {

	Request request;
	OutputStream output;

	public Response(OutputStream output) {
		this.output = output;
	}

	public void setRequest(Request request) {
		this.request = request;
	}

	public void sendStaticResource() throws IOException {
		if (request.getUri() == "" || request.getUri() == null) {
			return;
		}

		FileInputStream fis = null;
		try {

			StringBuilder response = new StringBuilder();
			buildHeader(response);

			response.append("{\"name\":\"wangfei\",\"age\":29}");

			output.write(response.toString().getBytes());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fis != null)
				fis.close();
		}
	}

	private void buildHeader(StringBuilder response) {
		response.append("HTTP/1.1 200 OK\r\n").append("Content-Type: text/plain;charset=utf-8\r\n")
				.append("\r\n");
	}
}